var crypto = require('crypto');
var utils = {
    getToken: function (headers) {
        if (headers && headers.authorization) {
            var authorization = headers.authorization;
            var part = authorization.split(' ');

            if (part.length == 2) {
                var token = part[1];

                return part[1];
            }
            else {
                return null;
            }
        }
        else {
            return null;
        }
    },
    randomWord: function (randomFlag, min, max) {
        var str = "",
            range = min,
            arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

        // 随机产生
        if (randomFlag) {
            range = Math.round(Math.random() * (max - min)) + min;
        }
        for (var i = 0; i < range; i++) {
            pos = Math.round(Math.random() * (arr.length - 1));
            str += arr[pos];
        }
        return str;
    },
    getEncAse192(str, secret) {
        var cipher = crypto.createCipher("aes192", secret); //设置加密类型 和 要使用的加密密钥
        var enc = cipher.update(str, "utf8", "hex");    //编码方式从utf-8转为hex;
        enc += cipher.final("hex"); //编码方式从转为hex;
        return enc;
    }

}
module.exports = utils;